<?php
namespace app\admin\controller;

class Attribute extends Common
{
    private $attr;
    private $attr_value;
    public function _initialize()
    {
        parent::_initialize();
        $this->attr = model('Attribute');
        $this->attr_value = model('AttrValue');
    }

    public function index()
    {
        $where = [];
        $data = $this->attr->getList($where);
        $this->assign('data', $data);
        return view();
    }

    public function save_attribute() {
        $id = input('param.id', 0);
        if (request()->isPost()) {
            /**
             * 组装数据
             */
            $data['name'] = input('post.name', '');
            $data['image'] = input('post.image', '');

            /**
             * 验证数据
             */
            $validate = validate('Attribute');
            if(!$validate->check($data)) {
                $this->error($validate->getError());
            }

            /**
             * 检查属性名是否存在
             */
            $where['id'] = $id > 0 ? ['neq', $id] : ['neq', 0];
            $where['name'] = $data['name'];
            $brand = $this->attr->get($where);
            if ($brand) {
                $this->error('属性名已存在');
            }

            /**
             * 保存数据
             */
            $where = $id > 0 ? ['id' => $id] : [];
            $rs = $this->attr->save($data, $where);
            if ($rs) {
                $this->success('保存成功', '');
            } else {
                $this->error('保存失败');
            }
        } else{
            $bl = input('param.bl', 1);
            $data = [];
            if ($id > 0) {
                $data = $this->attr->get(['id' => $id]);
            }
            $this->assign('data', $data);
            $this->assign('bl', $bl);
            return view();
        }
    }

    public function attr_value() {
        $attr_id = input('param.id', 0);
        if (!$attr_id) {
            $this->error('参数错误！');
        }
        $attr = $this->attr->get(['id' => $attr_id]);
        $this->assign('attr', $attr);

//        $where = ['attribute_id' => $attr_id];
//        $data = $this->attr_value->getList($where);
        $data = $attr->attrValue;
        $this->assign('data', $data);
        return view();
    }

    public function save_attr_value() {
        $id = input('param.id', 0);
        if (request()->isPost()) {
            /**
             * 组装数据
             */
            $data['name'] = input('post.name', '');
            $data['image'] = input('post.image', '');
            $data['attribute_id'] = input('post.attr_id', '');

            /**
             * 验证数据
             */
            $validate = validate('AttrValue');
            if(!$validate->check($data)) {
                $this->error($validate->getError());
            }

            /**
             * 检查属性值名是否存在
             */
            $where['id'] = $id > 0 ? ['neq', $id] : ['neq', 0];
            $where['name'] = $data['name'];
            $brand = $this->attr->get($where);
            if ($brand) {
                $this->error('属性值名已存在');
            }

            /**
             * 保存数据
             */
            $where = $id > 0 ? ['id' => $id] : [];
            $rs = $this->attr_value->save($data, $where);

            // 清理缓存
            \app\api\service\Base::clear_cache();

            if ($rs) {
                $this->success('保存成功', '');
            } else {
                $this->error('保存失败');
            }
        } else{
            $attr_id = input('param.attr_id', 0);
            $bl = input('param.bl', 1);
            $data = [];
            if ($id > 0) {
                $data = $this->attr_value->get(['id' => $id]);
                $attr_id = $data['attribute_id'];
            }
            $this->assign('attr_id', $attr_id);
            $this->assign('data', $data);
            $this->assign('bl', $bl);
            return view();
        }
    }
}
